/*
 *  Theroy0039_ClassifiedChords.cpp
 *  SoundController0002
 *
 *  Created by jiro on 11/07/17.
 *  Copyright 2011 jiro music. All rights reserved.
 *
 */

#include "Theroy0039_ClassifiedChords.h"
#include "../../sc/AllExceptions.h"

namespace theory0039 {

	void ClassifiedChords::AddId( const std::string& id )
	{
		classNames.push_back( id );
		chordsList.push_back( std::vector< Chord >() );
	}

	void ClassifiedChords::AddChord( const std::string& id, const Chord& chord )
	{
		int index = GetIndexOfId( id );
		if ( index == -1 )
		{
			throw sc::InvalidParameterException( "void ClassifiedChords::AddChord( const std::string& id, const Chord& chord )" );
		}
		else
		{
			chordsList[ index ].push_back( chord );
		}
	}


	void ClassifiedChords::AddChords( const std::string& id, const std::vector< Chord >& chords )
	{
		int index = GetIndexOfId( id );
		if ( index == -1 )
		{
			chordsList.push_back( chords );
			classNames.push_back( id );
		}
		else
		{
			std::copy( chords.begin(), chords.end(), std::back_inserter( chordsList[ index ] ) );
		}
	}

	void ClassifiedChords::SetChords( int index, const std::vector< Chord >& chords )
	{
		if ( index < 0 || index >= GetClassCount() )
		{
			throw sc::InvalidParameterException( "void ClassifiedChords::SetChords( int index, const std::vector< Chord >& chords )" );
		}
		chordsList[ index ] = chords;
	}

	int ClassifiedChords::GetIndexOfId( const std::string& id ) const
	{
		for ( int i = 0; i < classNames.size(); i++ )
		{
			if ( id == classNames[ i ] )
			{
				return i;
			}
		}
		return -1;
	}
	
	int ClassifiedChords::GetClassCount() const
	{
		return classNames.size();
	}
	
	std::string ClassifiedChords::GetClassNameOfIndex( int index ) const
	{
		return classNames[ index ];
	}

	const std::vector< Chord >& ClassifiedChords::GetChords( const std::string& id ) const
	{
		int index = GetIndexOfId( id );
		if ( index == -1 )
		{
			throw sc::InvalidParameterException( "const std::vector< Chord >& ClassifiedChords::GetChords( const std::string& id ) const" );
		}
		else
		{
			return chordsList[ index ];
		}
	}

	const std::vector< Chord >& ClassifiedChords::GetChords( int index ) const
	{
		if ( index >= GetClassCount() )
		{
			throw sc::InvalidParameterException( "const std::vector< Chord >& ClassifiedChords::GetChords( int index ) const" );
		}
		return chordsList[ index ];
	}
	
	void ClassifiedChords::Clear()
	{
		classNames.clear();
		chordsList.clear();
	}
	
}
